package cn.edu.nju.gitlab.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.nju.gitlab.R;
import cn.edu.nju.gitlab.activity.StudentActivity;
import cn.edu.nju.gitlab.injector.DaggerActivityComponent;
import cn.edu.nju.gitlab.injector.MainModule;
import cn.edu.nju.gitlab.model.Exam;
import cn.edu.nju.gitlab.model.Group;
import cn.edu.nju.gitlab.presenter.ClassListPresenter;
import cn.edu.nju.gitlab.activity.MainActivity;

/**
 * Created by lpt on 2017/6/7.
 * 班级列表
 */

public class ClassListFragment extends BaseFragment {
    @BindView(R.id.classlist)
    ListView listView;
    @Inject
    ClassListPresenter presenter;
    private List<Map<String, Object>> classMap;
    private  List<Group> grouplist;
    private SimpleAdapter adapter;

    public void showError() {
        baseError();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        activity = (MainActivity) getActivity();
        classMap=new ArrayList<>();
        view = inflater.inflate(R.layout.class_list, container, false);
        ButterKnife.bind(this, view);
        DaggerActivityComponent.builder()
                .mainModule(new MainModule(this))
                .build()
                .inject(this);


        MainActivity.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                classMap.clear();
                for (Group group : grouplist) {
                    if (group.getName().contains(newText)) {
                        putData(group);
                    }
                }
                adapter.notifyDataSetChanged();
                return true;
            }
        });
        presenter.getGroups(username, password);
        return view;
    }




    @Override
    public void onStop() {
        super.onStop();
        presenter.onStop();
    }

    public void showList(List<Group> groups) {


        this.grouplist = groups;
        for (Group group : grouplist) {
           // System.out.println(group.getId());
          //  System.out.println(group.getName());
            putData(group);
        }

        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                String[] from = {"id", "name"};
                int[] to = {R.id.class_title, R.id.class_content};
                 adapter = new SimpleAdapter(view.getContext(), classMap, R.layout.class_info, from, to);

                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(view.getContext(), StudentActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("group", grouplist.get(position));
                        intent.putExtras(bundle);
                        intent.putExtra("username", username);
                        intent.putExtra("password", password);
                        intent.putExtra("type", type);
                        intent.putExtra("userId", userId);
                        startActivity(intent);
                    }
                });
            }
        };
        activity.runOnUiThread(myRunnable);



    }


    private void putData(Group group) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", group.getId());
        map.put("name", group.getName());

        classMap.add(map);
    }
}

